package com.testing.class14;

import com.alibaba.fastjson.JSONPath;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Classname TokenTest
 * @Description 类型说明
 * @Date 2022/11/4 21:51
 * @Created by 特斯汀Roy
 */
public class TokenTest {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient client= HttpClients.createDefault();
        HttpPost auth=new HttpPost("http://www.testingedu.com.cn:8081/inter/HTTP/auth");
        String authResult = EntityUtils.toString(client.execute(auth).getEntity(), "utf-8");
        String tokenValue= JSONPath.read(authResult,"$.token").toString();
        System.out.println(authResult);
        System.out.println(tokenValue);

        HttpPost register=new HttpPost("http://www.testingedu.com.cn:8081/inter/HTTP//register?username=roy5&pwd=123456&nickname=aka柔弱&describe=肉一老师");
        register.setHeader("token",tokenValue);
        String registerResult = EntityUtils.toString(client.execute(register).getEntity(), "utf-8");
        System.out.println(registerResult);


        HttpPost login=new HttpPost("http://www.testingedu.com.cn:8081/inter/HTTP/login");
        StringEntity loginParam=new StringEntity("username=roy5&password=123456","utf-8");
        loginParam.setContentType("application/x-www-form-urlencoded");
        login.setEntity(loginParam);
        //使用token
        login.setHeader("token",tokenValue);
        login.setHeader("roy","hahaha");
        String loginRes = EntityUtils.toString(client.execute(login).getEntity(), "utf-8");
        System.out.println(loginRes);
        String idValue = JSONPath.read(loginRes, "$.userid").toString();


        HttpPost userInfo=new HttpPost("http://www.testingedu.com.cn:8081/inter/HTTP/getUserInfo");
        StringEntity userParam=new StringEntity("id="+idValue);
        userParam.setContentType("application/x-www-form-urlencoded");
        userInfo.setHeader("token",tokenValue);

        userInfo.setEntity(userParam);
        String userRes = EntityUtils.toString(client.execute(userInfo).getEntity(), "utf-8");
        System.out.println(userRes);


    }


}
